<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-1
 * Time: 上午11:38
 */

namespace App\Http\Controllers\Admin;



use App\Models;
use App\Repositories;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ConfigController extends AdminController
{

    /**
     * ConfigController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setTemplatePrefix(config('admin.systems.template_prefix', 'admin.').'config.');
    }

    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    public function getServiceRepository() : Repositories\Admin\ConfigRepository
    {
        if (!($this->serviceRepository instanceof Repositories\Admin\ConfigRepository)) {
            $this->serviceRepository = new Repositories\Admin\ConfigRepository();
        }

        return $this->serviceRepository;
    }

    /**
     * 配置项列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $data = $this->getServiceRepository()->getPaginateData();

        return $this->fetch('index', ['data'=>$data]);
    }

    /**
     * 更新配置项信息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        if (strtolower($request->method()) !== 'post') {
            $id = $request->route('id', 0);
            $detail = $this->getServiceRepository()->getSelectModel();
            $action = 'add';
            if (!empty($id)) {
                $detail = $this->getServiceRepository()->getDetailById($id);
                $action = 'edit';
            }
            return $this->fetch('edit', [
                'metaTitle' => $action == 'edit' ? '更新配置项信息' : '添加配置项信息',
                'postUrl'   => get_admin_url('config/edit', ['id'=>$id]),
                'detail'    => $detail,
                'action'    => $action,
                'id'        => $id
            ]);
        }

        $info = $request->input('info', []);

        $action = $request->input('action', 'add');
        $info['name'] = strtolower($info['name']);
        $this->getServiceRepository()->save($info, $action);

        return $this->success('更新配置项信息成功');
    }

    /**
     * 信息配置值
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function setting(Request $request)
    {
        if (strtolower($request->method()) !== 'post') {

            $isDeveloper = config('admin.systems.administrator_uid', 0) == get_admin_current_user('id') ? null : 0;

            $data = $this->getServiceRepository()->getPaginateData($request->input('group', 1), $isDeveloper);

            return $this->fetch('setting', ['data'=>$data, 'group'=>$request->input('group', 1)]);
        }

        $config = $request->input('config', []);

        foreach ($config as $key=>$value) {
            $info['id'] = $key;
            $info['value'] = is_array($value) ? json_encode($value) : $value;
            $this->getServiceRepository()->save($info, 'edit');
        }

        return $this->success('更新配置值成功');
    }
}